package com.vlyman.rtn;

import com.vlyman.rtn.consts.BaseRtn;
import com.vlyman.rtn.consts.IBaseRtn;

/**
 * 类描述：
 *  管理后台返回信息枚举类
 * @author chenjs
 * @date 2018/12/22 11:40
 **/
public enum SystemRtn implements IBaseRtn {
    /**
     * 系统错误枚举
     */
    NAME_INVALID("用户名不正确", 200000),
    PWD_INVALID("密码不正确", 200001),
    AUTH_MISSING("暂无权限", 200002),
    NOT_LOGIN("尚未登录", 200003),
    TOKEN_MISSING("缺少Authorization信息", 200004),
    TOKEN_INVALID("Token或者密钥不正确", 200005),
    TOKEN_EXPIRED("Token已过期", 200006),
    LOGIN_ERROR("登录操作出错", 200007),
    TOKEN_DECODE_ERROR("Token解析出错", 200008),
    CAPTCHA_EXPIRED("验证码超时", 200009),
    CAPTCHA_INVALID("验证码不正确", 200010),
    RSA_DECODE_ERROR("RSA解密出错", 200011),
    TOKEN_VALIDATE_ERROR("校验token出错", 200012);
    private String msg;
    private int code;

    SystemRtn(String msg, int code) {
        this.msg = msg;
        this.code = code;
    }

    @Override
    public String getMsg() {
        return msg;
    }

    @Override
    public int getCode() {
        return code;
    }

    public String getCodeStr(){
        return code+"";
    }

    public static IBaseRtn getRtnByCode(String codeStr) {
        int code;
        try {
            code = Integer.valueOf(codeStr);
        } catch (NumberFormatException e) {
            //对没有标识的错误，一律返回成请求失败
            return BaseRtn.ERROR;
        }
        for (SystemRtn item : SystemRtn.values()) {
            if (code == item.getCode()) {
                return item;
            }
        }
        //匹配不了错误码的也返回请求失败
        return BaseRtn.ERROR;
    }
}
